package com.sparrow.source.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sparrow.source.persistense.IasFaceSyncRes;
import com.sparrow.source.persistense.mapper.IasFaceSyncResMapper;
import com.sparrow.source.service.IIasFaceSyncResService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author bryceyung
 * @since 2024/11/23
 */
@Service
public class IasFaceSyncResServiceImpl extends ServiceImpl<IasFaceSyncResMapper, IasFaceSyncRes> implements IIasFaceSyncResService {

    @Override
    public Boolean saveSyncRes(IasFaceSyncRes syncRes) {
        List<IasFaceSyncRes> syncResList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(syncRes.getUserFaceIdMap())) {
            IasFaceSyncRes faceSyncRes = new IasFaceSyncRes();
            faceSyncRes.setUserId(0);
            faceSyncRes.setFaceId(0);
            faceSyncRes.setSyncTime(new Date());
            faceSyncRes.setDeviceId(syncRes.getDeviceId());
            faceSyncRes.setUnvResp(syncRes.getUnvResp());
            faceSyncRes.setSyncResult(syncRes.getSyncResult());
            syncResList.add(faceSyncRes);
        } else {
            syncRes.getUserFaceIdMap().forEach((k, v) -> {
                IasFaceSyncRes faceSyncRes = new IasFaceSyncRes();
                faceSyncRes.setUserId(k);
                faceSyncRes.setFaceId(v);
                faceSyncRes.setSyncTime(new Date());
                faceSyncRes.setDeviceId(syncRes.getDeviceId());
                faceSyncRes.setUnvResp(syncRes.getUnvResp());
                faceSyncRes.setSyncResult(syncRes.getSyncResult());
                syncResList.add(faceSyncRes);
            });
        }
        return baseMapper.batchInsert(syncResList);
    }
}
